using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace GameTank
{
    class Button : InvisibleGameEntity
    {

        Keys key;
        public Keys Key
        {
            get { return key; }
            set { key = value; }
        }

        bool isDown = false;
        bool oldDown = false;

        public Button(//Game game,//,Texture2D texture2, Vector2 npos
            Keys k
            )
            //: base(game)
        {
            //this.pos = npos;
            //texture = texture2;
            //rec = new Rectangle((int)pos.X, (int)pos.Y, texture.Width, texture.Height);
            this.Key = k;
            //base.LoadContent();
        }


        public override void Update(GameTime gameTime) 
        {
            oldDown = isDown;
            //TouchCollection touchLocations = TouchPanel.GetState();
            //color = Color.White;
            KeyboardState keyboard = Keyboard.GetState();
            isDown = false;

            if (keyboard.IsKeyDown(key)) // da nhan nut
            {
                isDown = true;
            }
            //foreach (TouchLocation touchLocation in touchLocations)
            //{

            //    if (touchLocation.State == TouchLocationState.Moved)
            //    {

            //        //Vector2 touchPosition = touchLocation.Position;

            //        if (CheckInside(new Vector2(rec.Center.X, rec.Center.Y), texture.Width / 2, touchLocation.Position))
            //        {
            //            isDown = true;
                        
            //            color = Color.FromNonPremultiplied(255,255,0,255);
            //        }
            //        else
            //        {
            //        }
            //    }
            //}
            //base.Update(gameTime);
        }

        private bool CheckInside(Vector2 tam, float R, Vector2 pos)
        {
            if (distance(tam,pos)<R)
                return true;
            return false;
        }

        double distance(Vector2 a, Vector2 b)
        {
            double d = Math.Sqrt((a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y));
            return d;
        }


        public bool IsDown
        {
            get
            {
                return isDown;
            }
        }

        public bool IsRelease
        {
            get
            {
                return !isDown;
            }
        }

        public bool IsDownBefore
        {
            get
            {
                return oldDown;
            }
        }

        public bool IsReleaseBefore
        {
            get
            {
                return !oldDown;
            }
        }

        public bool Press
        {
            get
            {
                return (IsDownBefore && IsRelease);
            }
        }
    }
}
